#include "customconvolutiondialog.h"

CustomConvolutionDialog::CustomConvolutionDialog(QWidget *parent) :
    QDialog(parent)
{

    setAttribute(Qt::WA_DeleteOnClose);
    setWindowTitle(tr("Custom Convolution Matrix"));

    QVBoxLayout *mainLayout = new QVBoxLayout(this);
    QGridLayout * middleLayout = new QGridLayout();
    QGridLayout * buttonLayout = new QGridLayout();

    QGroupBox *matrixBox = new QGroupBox(tr("Matrix"), this);
    QGridLayout *matrixLayout = new QGridLayout(matrixBox);

    m_1_1 = new QLineEdit(this);
    m_1_2 = new QLineEdit(this);
    m_1_3 = new QLineEdit(this);
    m_1_4 = new QLineEdit(this);
    m_1_5 = new QLineEdit(this);
    m_2_1 = new QLineEdit(this);
    m_2_2 = new QLineEdit(this);
    m_2_3 = new QLineEdit(this);
    m_2_4 = new QLineEdit(this);
    m_2_5 = new QLineEdit(this);
    m_3_1 = new QLineEdit(this);
    m_3_2 = new QLineEdit(this);
    m_3_3 = new QLineEdit(this);
    m_3_4 = new QLineEdit(this);
    m_3_5 = new QLineEdit(this);
    m_4_1 = new QLineEdit(this);
    m_4_2 = new QLineEdit(this);
    m_4_3 = new QLineEdit(this);
    m_4_4 = new QLineEdit(this);
    m_4_5 = new QLineEdit(this);
    m_5_1 = new QLineEdit(this);
    m_5_2 = new QLineEdit(this);
    m_5_3 = new QLineEdit(this);
    m_5_4 = new QLineEdit(this);
    m_5_5 = new QLineEdit(this);

    matrixLayout->addWidget(m_1_1, 0, 0);
    matrixLayout->addWidget(m_1_2, 0, 2);
    matrixLayout->addWidget(m_1_3, 0, 4);
    matrixLayout->addWidget(m_1_4, 0, 6);
    matrixLayout->addWidget(m_1_5, 0, 8);
    matrixLayout->addWidget(m_2_1, 2, 0);
    matrixLayout->addWidget(m_2_2, 2, 2);
    matrixLayout->addWidget(m_2_3, 2, 4);
    matrixLayout->addWidget(m_2_4, 2, 6);
    matrixLayout->addWidget(m_2_5, 2, 8);
    matrixLayout->addWidget(m_3_1, 4, 0);
    matrixLayout->addWidget(m_3_2, 4, 2);
    matrixLayout->addWidget(m_3_3, 4, 4);
    matrixLayout->addWidget(m_3_4, 4, 6);
    matrixLayout->addWidget(m_3_5, 4, 8);
    matrixLayout->addWidget(m_4_1, 6, 0);
    matrixLayout->addWidget(m_4_2, 6, 2);
    matrixLayout->addWidget(m_4_3, 6, 4);
    matrixLayout->addWidget(m_4_4, 6, 6);
    matrixLayout->addWidget(m_4_5, 6, 8);
    matrixLayout->addWidget(m_5_1, 8, 0);
    matrixLayout->addWidget(m_5_2, 8, 2);
    matrixLayout->addWidget(m_5_3, 8, 4);
    matrixLayout->addWidget(m_5_4, 8, 6);
    matrixLayout->addWidget(m_5_5, 8, 8);
    
    matrixBox->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);

    matrixList << m_1_1;
    matrixList << m_1_2;
    matrixList << m_1_3;
    matrixList << m_1_4;
    matrixList << m_1_5;
    matrixList << m_2_1;
    matrixList << m_2_2;
    matrixList << m_2_3;
    matrixList << m_2_4;
    matrixList << m_2_5;
    matrixList << m_3_1;
    matrixList << m_3_2;
    matrixList << m_3_3;
    matrixList << m_3_4;
    matrixList << m_3_5;
    matrixList << m_4_1;
    matrixList << m_4_2;
    matrixList << m_4_3;
    matrixList << m_4_4;
    matrixList << m_4_5;
    matrixList << m_5_1;
    matrixList << m_5_2;
    matrixList << m_5_3;
    matrixList << m_5_4;
    matrixList << m_5_5;

    QDoubleValidator *validator = new QDoubleValidator(this);
    foreach (QLineEdit *l, matrixList) {
        l->setText(tr("0"));
        l->setValidator(validator);
    }
    m_3_3->setText(tr("1"));

    middleLayout->addWidget(new QLabel(tr("Divisor:")), 0, 0);
    divisor = new QLineEdit("1", this);
    divisor->setMaximumWidth(50);
    divisor->setValidator(validator);
    middleLayout->addWidget(divisor, 0, 1);

    resetButton = new QPushButton(tr("Re&set"), this);
    cancelButton = new QPushButton(tr("&Cancel"), this);
    OKButton = new QPushButton(tr("&OK"), this);
    OKButton->setDefault(true);

    buttonLayout->addWidget(resetButton, 0, 0);
    buttonLayout->addWidget(cancelButton, 0, 1);
    buttonLayout->addWidget(OKButton, 0, 2);

    mainLayout->addWidget(matrixBox);
    mainLayout->addLayout(middleLayout);
    mainLayout->addSpacerItem(new QSpacerItem(0,0,QSizePolicy::Expanding, QSizePolicy::Expanding));
    mainLayout->addLayout(buttonLayout);

    this->resize(minimumSizeHint());

    connect(resetButton, SIGNAL(clicked()), this, SLOT(resetMatrix()));
    connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
    connect(OKButton, SIGNAL(clicked()), this, SLOT(acceptMatrix()));
}

void CustomConvolutionDialog::acceptMatrix()
{
    double matrix[5*5];
    int index = 0;
    bool ok, ok2;
    foreach (QLineEdit *l, matrixList) {
        matrix[index] = (double)l->text().toDouble(&ok);
        matrix[index] /= divisor->text().toDouble(&ok2);
        if (!ok || !ok2) {
            reject();
            close();
        }
        index++;
    }
    emit matrixAccepted(matrix, 5, 5);
    close();
}

void CustomConvolutionDialog::resetMatrix()
{
    foreach (QLineEdit *l, matrixList) {
        l->setText(tr("0"));
    }
    m_3_3->setText(tr("1"));
    divisor->setText(tr("1"));
}
